package com.ks.core.util;


import com.ks.core.exception.BizExceptionEnum;
import com.ks.core.exception.GunsException;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class StringUtil {

    /**
     * 获取数组的第一个元素
     *
     * @param strs
     * @param symbol
     * @return
     */
    public static String getFirstByArrays(String strs, String symbol) {
        if (ToolUtil.isEmpty(strs)) {
            return null;
        }
        String[] split = strs.split(symbol);
        if (split.length > 0) {
            return split[0];
        }
        return strs;
    }

    /**
     * 校验价格是否大于
     *
     * @return
     */
    public static boolean checkGt(BigDecimal priceOne, BigDecimal priceTwo) {
        int result = priceOne.compareTo(priceTwo);
        if (result > 0) {
            return true;
        }
        return false;
    }

    /**
     * 校验价格是否大于等于
     *
     * @return
     */
    public static boolean checkGe(BigDecimal priceOne, BigDecimal priceTwo) {
        int result = priceOne.compareTo(priceTwo);
        if (result >= 0) {
            return true;
        }
        return false;
    }

    public static int getOrderScore(int price) {
        return price % 10 == 0 ? price / 10 : price / 10 + 1;
    }

    /**
     * 校验价格是否小于
     *
     * @return
     */
    public static boolean checkLt(BigDecimal priceOne, BigDecimal priceTwo) {
        int result = priceOne.compareTo(priceTwo);
        if (result < 0) {
            return true;
        }
        return false;
    }

    /**
     * 校验价格是否小于等于
     *
     * @return
     */
    public static boolean checkLe(BigDecimal priceOne, BigDecimal priceTwo) {
        int result = priceOne.compareTo(priceTwo);
        if (result <= 0) {
            return true;
        }
        return false;
    }

    /**
     * 校验金额是否小于0
     *
     * @return
     */
    public static BigDecimal checkMoney(BigDecimal money) {
        int result = money.compareTo(BigDecimal.ZERO);
        if (result == -1) {
            log.info("money：{}", money);
            throw new GunsException(BizExceptionEnum.MONEY_ERROR);
        } else if (result == 0) {
            log.info("money：{}", money);
            throw new GunsException(BizExceptionEnum.MONEY_ERROR);
        } else {
            return money;
        }
    }


    /**
     * 校验金额是否等于0
     *
     * @return
     */
    public static boolean checkMoneyIsZero(BigDecimal money) {
        int result = money.compareTo(BigDecimal.ZERO);
        if (result == 0) {
            return true;
        }
        return false;
    }

    /**
     * 校验两个数字是否相等
     *
     * @return
     */
    public static boolean checkMoneyEquals(BigDecimal moneyOne, BigDecimal moneyTwo) {
        checkMoney(moneyOne);
        checkMoney(moneyTwo);
        return moneyOne.compareTo(moneyTwo) == 0 ? true : false;
    }

    /**
     * 生成随机数字和字母
     *
     * @param length 字符串长度
     * @return
     */
    public static String getStringRandom(int length) {
        String val = "";
        Random random = new Random();
        //参数length，表示生成几位随机数
        for (int i = 0; i < length; i++) {

            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if ("char".equalsIgnoreCase(charOrNum)) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char) (random.nextInt(26) + temp);
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }

    /**
     * 获得随机数，[x,y)
     *
     * @param min
     * @param max
     * @return
     */
    public static int getRandom(int min, int max) {
//        max = max++;
        Random random = new Random();
        int s = random.nextInt(max) % (max - min + 1) + min;
        return s;

    }

    /**
     * 获取UUID
     *
     * @return
     */
    public static String getUUID() {
        return UUID.randomUUID().toString();
    }

    public static String getAccountIdByUUId() {
        //最大支持1-9个集群机器部署
        int machineId = 1;
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        //有可能是负数
        if (hashCodeV < 0) {
            hashCodeV = -hashCodeV;
        }
        return machineId + String.format("%015d", hashCodeV);
    }

    public static int getScoreLevel(int score) {
        if (score < 30) {
            return 0;
        } else if (30 <= score && score < 70) {
            return 1;
        } else if (70 <= score && score < 120) {
            return 2;
        } else if (120 <= score && score < 180) {
            return 3;
        } else if (180 <= score && score < 260) {
            return 4;
        } else if (260 <= score && score < 360) {
            return 5;
        } else if (360 <= score && score < 480) {
            return 6;
        } else if (480 <= score && score < 620) {
            return 7;
        } else if (620 <= score && score < 780) {
            return 8;
        } else if (780 <= score && score < 1080) {
            return 9;
        } else if (1080 <= score && score < 1500) {
            return 10;
        } else if (1500 <= score && score < 2100) {
            return 11;
        } else if (2100 <= score && score < 3000) {
            return 12;
        } else if (3000 <= score && score < 4300) {
            return 13;
        } else if (4300 <= score && score < 6000) {
            return 14;
        } else if (6000 <= score) {
            return 15;
        }
        return 0;
    }

    /**
     * 计算成长值
     *
     * @param price
     * @param mul   倍率
     * @return
     */
    public static int getGrowth(BigDecimal price, int mul) {
        return (int) Math.ceil(price.setScale(0, BigDecimal.ROUND_UP).doubleValue() / mul);
    }

    final static Pattern p = Pattern.compile("(\\{[^\\}]*\\})");

    public static Set<String> extractMessageByRegular(String msg) {
        Set<String> keys = new HashSet<>();
        Matcher m = p.matcher(msg);
        while (m.find()) {
            keys.add(m.group().substring(1, m.group().length() - 1));
        }
        return keys;
    }

    public static String getGoodsPriceId(@NonNull Integer goodsId, @NonNull String goodsPropertyIds) {
        StringBuffer goodsPriceId = new StringBuffer(String.valueOf(goodsId)).append("-");
        String[] split = goodsPropertyIds.split(",");
        for (String s : split) {
            goodsPriceId.append(s).append("-");
        }
        return goodsPriceId.substring(0, goodsPriceId.length() - 1);
    }

    /**
     * 邮箱正则
     *
     * @param email
     * @return
     */
    public static boolean isEmail(String email) {
        String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(email);
        return matcher.matches();
    }

    /**
     * 手机号正则
     *
     * @param phone
     * @param force 强制检查
     * @return
     */
    public static boolean isPhone(String phone, boolean force) {
        String check = "^(((13[0-9])|(14[579])|(15([0-3]|[5-9]))|(16[6])|(17[0135678])|(18[0-9])|(19[89]))\\d{8})$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(phone);
        boolean result = matcher.matches();
        if (force) {
            if (!result) {
                throw new GunsException(BizExceptionEnum.MOBILE_ERROR);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        String stringRandom = getStringRandom(8);
        System.out.println(stringRandom);
    }
}
